package com.calefk.util {
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.events.Event;
	import flash.events.IOErrorEvent;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;

    public class DisplayLoader {
        
        private var mLoader:Loader;
        private var mCanvas:MovieClip;
        private var mClip:MovieClip;
        private var mBitmapData:BitmapData;
        private var mBitmap:Bitmap;
        private var mContext:LoaderContext;
        
        private var mWidth :Number;
        private var mHeight:Number;
        
        public var onComplete:Function;
        
        public function get clip():MovieClip { return mClip; }
        public function get canvas():MovieClip { return mCanvas; }
        public function get bitmapData():BitmapData { return mBitmapData; }
        public function get height():Number { return mHeight; }
        public function get width():Number { return mWidth; }
        
        public function DisplayLoader(canvas:MovieClip , path:String , px:Number = 0, py:Number = 0 , width:Number = 0 , height:Number = 0 , context:LoaderContext = null) {
            mCanvas = canvas;
            mContext = context;
            
            mWidth = width;
            mHeight = height;
            
            mLoader = new Loader();
            var asset:URLRequest = new URLRequest(path);

            mLoader.load(asset , mContext);
            
            mLoader.contentLoaderInfo.addEventListener(Event.INIT , onCompleteInternal);
            mLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , onError);
            mClip = new MovieClip();
            
            mClip.x = px;
            mClip.y = py;
        }
		
		private function onError(e:IOErrorEvent):void {
			trace( "onError DisplayLoader : " + e );
		}
        
        private function onCompleteInternal(e:Event):void {
            // remove listener
            mLoader.contentLoaderInfo.removeEventListener(Event.INIT , onCompleteInternal);
            // get bitmap data
            mBitmapData = Bitmap(mLoader.content).bitmapData;
            
            // create new bitmap
            mBitmap = new Bitmap(mBitmapData);
			
			// asign size default
			if (mWidth != 0 && mHeight != 0) {
				mBitmap.width  = mWidth;
				mBitmap.height = mHeight;
			}
			
            mClip.addChild(mBitmap);
            mCanvas.addChild(mClip);
            
            // call function
            if (onComplete != null) {
                onComplete();
            }
        }

        public function clean() {        
            if (mBitmap) {
                mClip.removeChild(mBitmap);
            }
            if (mClip) {
                mCanvas.removeChild(mClip);
            }
            if (mLoader) {
                mLoader.contentLoaderInfo.removeEventListener(Event.INIT , onCompleteInternal);
                mLoader.unload();
                mLoader = null;
            }
            mBitmap = null;
            mBitmapData = null;
            mClip = null;
            mCanvas = null;
            mContext = null;
        }
    }

}